<template>
  <div :class="['tag', {text: !button, btn: button}]" @contextmenu.stop.prevent @touchstart.stop="touchstartHandle" @touchend.stop="touchendHandle">
    <span>{{ label }}</span>
    <template v-if="close && button">
      <span class="line" />
      <svg-icon class="icon" icon-class="cuowuguanbiquxiao" @click.native.stop="closeHandle" @touchend.native.stop />
    </template>
  </div>
</template>
<script>
export default {
  name: 'Tag',
  props: {
    label: {
      type: String,
      default: ''
    },
    value: {
      type: [String, Number, Object, Array],
      default: ''
    },
    button: { // 按钮形式
      type: Boolean,
      default: false
    },
    closeable: { // 能否关闭 按钮形式生效
      type: Boolean,
      default: false
    },
    longPress: { // 是否长按显示关闭按钮
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      longPressed: false,
      longTime: 0
    }
  },
  computed: {
    close() {
      return this.longPress ? this.longPressed && this.closeable : this.closeable
    }
  },
  methods: {
    closeHandle() {
      this.$emit('close')
    },
    clickHandle() {
      this.$emit('click', this.value)
    },
    touchstartHandle(e) {
      this.longTime = new Date().getTime()
    },
    touchendHandle(e) {
      const duration = new Date().getTime() - this.longTime
      this.longTime = 0
      if (duration > 500) {
        this.longPressed = true
      } else {
        this.clickHandle()
      }
    }
  }
}
</script>
<style lang="less" scoped>
.tag{
  height: 30px;
  line-height: 30px;
  display: inline-block;
  user-select: none;
  .icon{
    font-size: 10px;
  }
  & *{
    user-select: none;
  }
}
.line{
  display: inline-block;
  height: 65%;
  width: 1px;
  background-color: rgba(189, 189, 189, 1);
  margin: 0 10px;
}
.text{
  color: rgba(153, 153, 153, 1);
  font-size: 14px;
}
.btn{
  background-color: rgba(246, 247, 247, 1);
  color: rgba(51, 51, 51, 1);
  font-size: 12px;
  padding: 0px 10px;
  border-radius: 2px;
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
}
</style>
